package com.sdzee.test;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;

public class MySQLAccessUSER {
	
  private Connection connect = null;
  private Statement statement = null;
  private PreparedStatement preparedStatement = null;
  private ResultSet resultSet = null;
  String url = "jdbc:mysql://82.224.100.119:3306/SMV";
  String utilisateur = "root";
  String motDePasse = "Smv2014";
  String[] strs = null;
  Date dateCreation = null;
  String Login = null;
  String Mdp = null;
  String Prenom = null;
  String Nom = null;
  String Ville = null;
  int NbEvenementCree =0;
  int NbEvenementParticipe=0;
  int NoteMoyenne=0;
  int Signale=0;
  String UrlPhoto=null;
  Date dateDernierEvenement=null;

  public void insertDataBase(String mess) throws Exception {
	  
	 strs = mess.split("/");	  
     Login = strs[0];;
     Mdp = "pwd";
     Prenom = strs[2];;
     Nom = strs[1];;
     Ville = strs[3];;
     UrlPhoto="urlPhoto";     

    try {
  
      Class.forName("com.mysql.jdbc.Driver");    
      connect = DriverManager.getConnection( url, utilisateur, motDePasse ); 
      statement = connect.createStatement();   
      preparedStatement = connect.prepareStatement("insert into  SMV.User values (default, ?,?,?,?,?,?,?,?,?,?,?,?)");
      preparedStatement.setDate(1, getCurrentDate());
      preparedStatement.setString(2, Login);
      preparedStatement.setString(3, Mdp);
      preparedStatement.setString(4, Prenom);
      preparedStatement.setString(5, Nom);
      preparedStatement.setString(6, Ville);
      preparedStatement.setInt(7, 0);
      preparedStatement.setInt(8, 0);
      preparedStatement.setInt(9, 0);
      preparedStatement.setInt(10, 0);
      preparedStatement.setString(11, UrlPhoto);
      preparedStatement.setDate(12, getCurrentDate());      
      preparedStatement.executeUpdate();     

            
    } catch (Exception e) {
      throw e;
    } finally {
      close();
    }

  }
  
  public ResultSet readDataBase() throws Exception{
	  
	  try {
	
	      Class.forName("com.mysql.jdbc.Driver");
	      connect = DriverManager.getConnection( url, utilisateur, motDePasse );	
	      statement = connect.createStatement();
	      resultSet = statement.executeQuery("select * from SMV.Evenement order by id desc" );
	     
	  } catch (Exception e) {
	      throw e;
	    } finally {
	     
	    }
	  return resultSet;
	  }
	  

 
  private void close() {
    try {
      if (resultSet != null) {
        resultSet.close();
      }

      if (statement != null) {
        statement.close();
      }

      if (connect != null) {
        connect.close();
      }
    } catch (Exception e) {

    }
  }
  
  private static java.sql.Date getCurrentDate() {
	    java.util.Date today = new java.util.Date();
	    return new java.sql.Date(today.getTime());
	}
} 
